package cn.iocoder.yudao.module.tms.service.bandinfo;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.tms.controller.admin.bandinfo.vo.BandInfoPageReqVO;
import cn.iocoder.yudao.module.tms.controller.admin.bandinfo.vo.BandInfoSaveReqVO;
import cn.iocoder.yudao.module.tms.dal.dataobject.bandinfo.BandInfoDO;
import cn.iocoder.yudao.module.tms.dal.mysql.bandinfo.BandInfoMapper;
import cn.iocoder.yudao.module.tms.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 公司band信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BandInfoServiceImpl implements BandInfoService {

    @Resource
    private BandInfoMapper bandInfoMapper;

    @Override
    public Long createBandInfo(BandInfoSaveReqVO createReqVO) {
        // 插入
        BandInfoDO bandInfo = BeanUtils.toBean(createReqVO, BandInfoDO.class);
        bandInfoMapper.insert(bandInfo);

        // 返回
        return bandInfo.getId();
    }

    @Override
    public void updateBandInfo(BandInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateBandInfoExists(updateReqVO.getId());
        // 更新
        BandInfoDO updateObj = BeanUtils.toBean(updateReqVO, BandInfoDO.class);
        bandInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteBandInfo(Long id) {
        // 校验存在
        validateBandInfoExists(id);
        // 删除
        bandInfoMapper.deleteById(id);
    }

    private void validateBandInfoExists(Long id) {
        if (bandInfoMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.BAND_INFO_NOT_EXISTS);
        }
    }

    @Override
    public BandInfoDO getBandInfo(Long id) {
        return bandInfoMapper.selectById(id);
    }

    @Override
    public PageResult<BandInfoDO> getBandInfoPage(BandInfoPageReqVO pageReqVO) {
        return bandInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public BandInfoDO getBandInfoByCode(String code) {
        return bandInfoMapper.selectOne(BandInfoDO::getCode, code);
    }
}